<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数字累加器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            background-color: #f0f0f0;
        }

        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 500px;
            margin-top: 50px;
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        input {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border: 2px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        #result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            display: none;
        }

        .success {
            background-color: #dff0d8;
            color: #3c763d;
            border: 1px solid #d6e9c6;
        }

        .error {
            background-color: #f2dede;
            color: #a94442;
            border: 1px solid #ebccd1;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>数字累加计算器</h1>
        <div class="input-group">
            <input type="text" id="numberInput" placeholder="请输入一个正整数">
        </div>
        <button onclick="calculateSum()">计算累加和</button>
        <div id="result"></div>
    </div>

    <script>
        function calculateSum() {
            const input = document.getElementById('numberInput').value.trim();
            const resultDiv = document.getElementById('result');
            
            // 输入验证
            if (!input) {
                showError("请输入一个数字");
                return;
            }

            const number = Number(input);
            
            if (isNaN(number)) {
                showError("请输入有效的数字");
                return;
            }

            if (!Number.isInteger(number) || number <= 0) {
                showError("请输入正整数");
                return;
            }

            // 计算累加和
            const sum = (number * (number + 1)) / 2;
            showSuccess(`1 到 ${number} 的累加和为：${sum}`);
        }

        function showError(message) {
            const resultDiv = document.getElementById('result');
            resultDiv.className = "error";
            resultDiv.innerHTML = message;
            resultDiv.style.display = "block";
        }

        function showSuccess(message) {
            const resultDiv = document.getElementById('result');
            resultDiv.className = "success";
            resultDiv.innerHTML = message;
            resultDiv.style.display = "block";
        }

        // 添加回车键支持
        document.getElementById('numberInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                calculateSum();
            }
        });
    </script>
</body>
</html>